<?php 
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-07-10 23:59:20 +0200 (Thu, 10 Jul 2008) $
 * $Rev: 1285 $
 * $Author: gavinspearhead $
 * $Id: ajax_controltransfers.php 1285 2008-07-10 21:59:20Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);
$__auth = 'silent';

$pathact = realpath(dirname(__FILE__));

require_once "$pathact/../functions/functions.php";
require_once "$pathact/../functions/html_includes.php";


if (!isset($_POST['cmd']))
	die ('Command not set');

$cmd = strtolower($_POST['cmd']);

$db->escape($cmd);

$prefs = load_config($db);
$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);
verify_challenge_text($_POST['challenge']);
	
// Safety check: TODO

switch ($cmd) {
	case 'clear' : 
		// Clear completed downloads:
		if ($uc->is_connected())
			$uc->cleandb('now');
		else
			die_html($LN['error_urddconnect']);
		break;
	case 'pause' : 
		// Pause everything:
		if ($uc->is_connected())
			$uc->pause('all');
		else
			die_html($LN['error_urddconnect']);
		break;
	case 'continue' :
		// Resume everything:
		if ($uc->is_connected())
			$uc->continue_cmd('all');
		else
			die_html($LN['error_urddconnect']);
		break;
	case 'poweron' : 
		// Turn URDD on
		start_urdd(); 
		break;
	case 'poweroff' : 
		// Turn URDD off
		stop_urdd();
		break;
	default:	
		die('Invalid command!');
}

// Success:
echo 'OK';
?>
